package com.hs.security.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.security.exception.ProjectException;
import com.hs.security.pojo.TbUser;
import com.hs.security.service.FileUploadService;
import com.hs.security.service.TbUserRoleService;
import com.hs.security.service.TbUserService;
import com.hs.security.utils.Constant;
import com.hs.security.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

/**
 * @ClassName UserController
 * @Description TODO
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    TbUserService service;
    @Autowired
    TbUserRoleService userRoleService;
    @Autowired
    FileUploadService fileUploadService;

    @PostMapping("/login")
    public Result login(@RequestBody TbUser user){
        service.login(user);
        return Result.success(user);
    }

    @GetMapping("/selectUsers/{pageNum}/{pageSize}")
    public Result selectUsers(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        Page<TbUser> page = service.findByPage(pageNum, pageSize);
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());
        map.put("totalPage",page.getPages());
        return Result.success(map);
    }

    @PostMapping("/insertUser")
    public Result addUser(@RequestBody TbUser user){
        //user.setPassword(new SimpleHash("md5",user.getPassword(),user.getSalt(),2).toHex());
        service.save(user);
        return new Result(Constant.CREATED.getCode(),Constant.CREATED.getMsg(),null);
    }


    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody TbUser user){
        TbUser tbUser = service.getById(user.getId());
        if (!tbUser.getPassword().equals(user.getPassword())){
            //user.setPassword(new SimpleHash("md5",user.getPassword(),null,2).toHex());
        }
        if(!service.updateById(user)){
            throw new ProjectException(Constant.UPDATE_ERR.getMsg(), Constant.UPDATE_ERR.getCode());
        }
        return new Result(Constant.UPDATE.getCode(),Constant.UPDATE.getMsg(),null);
    }

    @GetMapping("/selectUser/{id}")
    public Result fidnById(@PathVariable Integer id){
        TbUser user = service.getById(id);
        return Result.success(user);
    }

    @DeleteMapping("/deleteUser/{id}")
    public Result deleteUserById(@PathVariable Integer id){
        if(!service.removeById(id)){
            throw new ProjectException(Constant.DELETED.getMsg(),Constant.DELETED.getCode());
        }
        return Result.success(Constant.DELETED.getMsg(), Constant.DELETED.getCode());
    }

    /**
     * 用户上传头像
     * @param file
     * @return com.hs.shirodemo01.utils.Result
     */
    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestBody MultipartFile file) throws IOException {
        String path = fileUploadService.uploadOneFile(file);
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("img","d:/"+path);
        return new Result(200,"文件上传成功",map);
    }

    @PostMapping("/updateUserRole")
    public Result updateUserRole(@RequestBody TbUser user){
        userRoleService.updateUserRoles(user);
        return new Result(Constant.CREATED.getCode(), "分配角色成功",null);
    }
}
